/**************************************************************************
*  Black Knight - Chess Engine                                            *
*  Copyright (C) 2009  Victor Rusu                                        *
*                                                                         *
*  This program is free software: you can redistribute it and/or modify   *
*  it under the terms of the GNU General Public License as published by   *
*  the Free Software Foundation, either version 3 of the License, or      *
*  (at your option) any later version.                                    *
*                                                                         *
*  This program is distributed in the hope that it will be useful,        *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           *
*  GNU General Public License for more details.                           *
*                                                                         *
*  You should have received a copy of the GNU General Public License      *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
**************************************************************************/

#include "io/read.h"

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>

Read* Read::read_instance_ = NULL;
const int Read::kMaxLineSize = 128;
boost::mutex Read::instance_lock_;
boost::mutex Read::read_lock_;

Read::Read() {
}

Read::~Read() {
}

Read* Read::instance() {
    instance_lock_.lock();
    if (read_instance_ == NULL) {
        read_instance_ = new Read;
    }
    instance_lock_.unlock();

    return read_instance_;
}

std::string Read::get_command() {

    Read::read_lock_.lock();

    char* command = new char[kMaxLineSize];
    std::cin.getline(command, kMaxLineSize);
    std::string return_value = command;
    delete[] command;

    Read::read_lock_.unlock();
    return return_value;
}
